const app = getApp();
var openid = wx.getStorageSync('openid');
Page({
  data: {
    tab: 0,
    winHeight: 100,
    textarea: '请输入内容...',
    imagesList: [],
    oneHasImage: false,
    twoCountImage: 0,
  },
  tab_slide: function(e) { //滑动切换tab 
    var that = this;
    that.setData({
      tab: e.detail.current
    });
  },
  tab_click: function(e) { //点击tab切换
    var that = this;
    if (that.data.tab === e.target.dataset.current) {
      return false;
    } else {
      that.setData({
        tab: e.target.dataset.current
      })
    }
  },
  onShow: function(e) {
    var that = this;
    that.getSystemInfo();
  },
  getSystemInfo: function() {
    var self = this
    wx.getSystemInfo({
      success: res => {
        self.setData({
          winHeight: res.windowHeight,
        })
      }
    })
  },
  //输入框获得聚焦
  textInput: function() {
    var that = this;
    that.setData({
      textarea: '',
    })
  },
  //输入框失去聚焦进行赋值
  textOutput: function(e) {
    var that = this;
    that.setData({
      textarea: e.detail.value,
    })
  },
  //输入框改变时动态的对文本域进行赋值
  changeInput: function(e) {
    let changed = {};
    let prop = e.currentTarget.dataset.prop
    changed[prop] = e.detail.value;
    this.setData(changed)
  },
  //上传图片
  uploader: function(event) {
    var that = this;
    var count = event.currentTarget.dataset.count;
    let maxSize = 1920 * 1920;
    let flag = true;
    wx.chooseImage({
      sizeType: ['compressed'], //['original', 'compressed'],可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function(res) {
        wx.showToast({
          title: '正在上传...',
          icon: 'loading',
          mask: true,
          duration: 500
        })
        //验证图片大小
        for (let i = 0; i < res.tempFiles.length; i++) {
          if (res.tempFiles[i].size > maxSize) {
            flag = false;
            wx.showModal({
              content: '图片太大，不允许上传',
              showCancel: false,
              success: function(res) {
                if (res.confirm) {
                  console.log('图片太大点击确定')
                }
              }
            });
          }
        }
        //验证图片
        if (res.tempFiles.length > count) {
          wx.showModal({
            content: '最多能上传' + count + '张图片',
            showCancel: false,
            success: function(res) {
              if (res.confirm) {
                console.log('图片太多点击确定');
              }
            }
          })
        }
        if (flag == true && res.tempFiles.length <= count) {
          that.data.imagesList[that.data.imagesList.length] = res.tempFilePaths;
          if (count == 9) {
            that.data.twoCountImage += 1;
          }
          that.setData({
            imagesList: that.data.imagesList,
            oneHasImage: true,
            twoCountImage: that.data.twoCountImage,
          })
        }
      },
      fail: function(res) {
        console.log(res);
      }
    })
  },
  //上传图片-取消选择框
  uploaderCancel: function(e) {
    var that = this;
    if (that.data.tab == 0) {
      that.setData({
        imagesList: [],
        oneHasImage: false
      })
    } else if (that.data.tab == 1) {
      that.data.imagesList.splice(e.target.id, 1);
      that.setData({
        imagesList: that.data.imagesList,
        oneHasImage: false,
        twoCountImage: that.data.twoCountImage - 1
      })
    }
  },
  //信息提交上传
  submit: function(res) {
    //登录状态
    openid = wx.getStorageSync('openid');
    var common = require('../../utils/common.js');
    if (!openid) {
      common.getToLogin();
      return false;
    }
    var that = this;
    if (openid) {
      if (that.data.textarea == '' || that.data.textarea == '请输入内容...') {
        wx.showToast({
          title: '内容不能为空',
          icon: 'none',
          duration: 1000
        });
        return false;
      }
    }
    var id = false;
    wx.showLoading({
      title: '上传中...',
    })
    wx.request({
      url: app.config.serverCfg.domain.dynamicAdd,
      method: 'post',
      header: {
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      data: {
        did: '',
        openid: openid,
        text: that.data.textarea,
        imagesList: that.data.imagesList,
        type: that.data.tab,
      },
      success: function(res) {
        if (res.data.data) {
          for (let i = 0; i < that.data.imagesList.length; i++) {
            wx.uploadFile({
              url: app.config.serverCfg.domain.dynamicAdd + '?did=' + res.data.data,
              filePath: that.data.imagesList[i][0],
              name: 'images',
              header: {
                "Content-Type": "multipart/form-data"
              },
              success: function(data) {},
              fail: function(data) {
                console.log('submit error:' + data);
              }
            });
          }
          if (res.data.data) {
            wx.hideLoading();
            wx.showModal({
              title: '提示',
              content: '上传成功',
              confirmText: '去查看',
              cancelText: '继续添加',
              success(res) {
                if (res.confirm) {
                  that.setData({
                    imagesList: [],
                    textarea: '请输入内容...',
                    tab: 0,
                    oneHasImage: false,
                  });
                  wx.switchTab({
                    url: '/pages/dynamic/index',
                  })
                } else if (res.cancel) {
                  that.setData({
                    imagesList: [],
                    textarea: '请输入内容...',
                    tab: 0,
                    oneHasImage: false,
                  });
                }
              }
            })
          }
        }
      }
    });
  },
  //信息提交上传
  submit2: function(res) {
    //登录状态
    openid = wx.getStorageSync('openid');
    var common = require('../../utils/common.js');
    if (!openid) {
      common.getToLogin();
      return false;
    }
    //内容content
    var that = this;
    if (openid) {
      if (that.data.textarea == '' || that.data.textarea == '请输入内容...') {
        wx.showToast({
          title: '内容不能为空',
          icon: 'none',
          duration: 1000
        });
        return false;
      }
    }
    var id = false;
    wx.showLoading({
      title: '上传中...',
    })
    wx.request({
      url: app.config.serverCfg.domain.dynamicAdd,
      method: 'post',
      header: {
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      data: {
        did: '',
        openid: openid,
        text: that.data.textarea,
        imagesList: that.data.imagesList,
        type: that.data.tab,
      },
      success: function(res) {
        if (res.data.data) {
          for (let i = 0; i < that.data.imagesList.length; i++) {
            wx.uploadFile({
              url: app.config.serverCfg.domain.dynamicAdd + '?did=' + res.data.data,
              filePath: that.data.imagesList[i][0],
              name: 'images',
              header: {
                "Content-Type": "multipart/form-data"
              },
              success: function(data) {
                if ((that.data.imagesList.length - 1) == i) {
                  wx.hideLoading();
                  wx.showModal({
                    title: '提示',
                    content: '上传成功',
                    confirmText: '去查看',
                    cancelText: '继续添加',
                    success(res) {
                      if (res.confirm) {
                        that.setData({
                          imagesList: [],
                          textarea: '请输入内容...',
                          tab: 1,
                          oneHasImage: false,
                          twoCountImage: 0,
                        });
                        wx.switchTab({
                          url: '/pages/dynamic/index'
                        })
                      } else if (res.cancel) {
                        that.setData({
                          imagesList: [],
                          textarea: '请输入内容...',
                          tab: 1,
                          oneHasImage: false,
                          twoCountImage: 0,
                        });
                      }
                    }
                  })
                }
              },
              fail: function(data) {
                console.log('submit error:' + data);
              }
            });
          }
        }
      }
    });
  }
})